package com.cognizant.ams.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cognizant.ams.bean.Lduser;
import com.cognizant.ams.bean.LduserExample;
import com.cognizant.ams.bean.LduserExample.Criteria;
import com.cognizant.ams.common.ldap.LDAPUtil;
import com.cognizant.ams.dao.LduserMapper;

@Service
public class LogonService {

	@Autowired
	private LduserMapper lduserMapper;

	public boolean checkUser(String username, String password) {
		LduserExample lduserExample = new LduserExample();
		Criteria criteria = lduserExample.createCriteria();
		criteria.andUsercodeEqualTo(username);
		List<Lduser> ldusers = lduserMapper.selectByExample(lduserExample);
		if (ldusers.size() > 0) {
			String flag=ldusers.get(0).getCertifyflag();
			if (flag!=null) {	
			if (flag.equals("0")) {
				if (password.equals(ldusers.get(0).getPassword())) {
					return true;
				}
			} else { // 非系统用户走ldap
				if (LDAPUtil.checkAD(username, password)) {
					return true;
				}
			}
			}
		}
		return false;
	}
}
